<?php

namespace Proyecto\MainBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Proyecto\MainBundle\Entity\Plantilla;
use Proyecto\MainBundle\Form\PlantillaType;

/**
 * Plantilla controller.
 *
 * @Route("/admin/plantilla")
 */
class PlantillaController extends Controller
{
    
   public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('ProyectoMainBundle:Plantilla')->findAll();

        return array(
            'entities' => $entities,
        );
    }

    /**
     * Creates a new Plantilla entity.
     *
     * @Route("/", name="plantilla_create")
     * @Method("POST")
     * @Template("ProyectoMainBundle:Plantilla:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity  = new Plantilla();
        $form = $this->createForm(new PlantillaType(), $entity);
        $form->bind($request);

        $resultado=null;
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();
            $resultado= 'La plantilla se creó con éxito.';
        }
        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
            'resultado' => $resultado,
        );
    }

     /**
     * Muestra el formulario para modificar la plantilla y sus elementos.
     *
     * @Route("/modificar/{id}/{current_page}/{errores}", name="plantilla_modificar", defaults= {"errores" = null, "current_page" = 1})
     * @Method("GET")
     * @Template("ProyectoMainBundle:Plantilla:edit.html.twig")
     */
    public function modificarAction($id,$current_page,$errores){
        $forms_elemento=$this->get("elemento.controller")->formsElementos($id);
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('ProyectoMainBundle:Plantilla')->find($id);
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Plantilla entity.');
        }
        $deleteFormPlantilla = $this->createDeleteForm($id);
        $editFormPlantilla = $this->createForm(new PlantillaType(), $entity);
        return array(
            'entity'      => $entity,
            'edit_form_plantilla'   => $editFormPlantilla->createView(),
            'delete_form_plantilla'   => $deleteFormPlantilla->createView(),
            'forms_elemento' => $forms_elemento,
            'current_page' => $this->getRequest()->query->get('current_page', $current_page),
            'errores' => $errores,
        );
        
    }
    
    /**
     * Lista todas las plantillas para editar.
     *
     * @Route("/listar/{current_page}", name="plantilla_listar",defaults={"current_page" = 1})
     * @Method("GET")
     * @Template("ProyectoMainBundle:Plantilla:listar.html.twig")
     */
    public function listarAction($current_page){
	$em=$this->getDoctrine()->getmanager();
	$consulta="Select p From ProyectoMainBundle:Plantilla p";
	$query = $em->createQuery($consulta);
	$paginator = $this->get('knp_paginator');
        if (!is_numeric($current_page)){
            $current_page=1;
        }
        $configurador=$this->get('configurador');
        $cant = $configurador->getElementosPorPagina('plantillas');
	$pagination= $paginator->paginate($query,$this->getRequest()->query->get('page', $current_page),
	$cant);
        $forms_plantilla=array();
        $i=1;
        foreach ($pagination->getItems() as $plantilla){
            $forms_plantilla[$i]['plantilla']=$plantilla;
            $forms_plantilla[$i]['delete_form']=$this->createDeleteForm($plantilla->getId())->createView();
            $i++;
        }
        $pagination->setItems($forms_plantilla);
        return array(
            'pagination' => $pagination,
        );
    }
    
    
    /**
     * Displays a form to create a new Plantilla entity.
     *
     * @Route("/new", name="plantilla_new")
     * @Method("GET")
     * @Template("ProyectoMainBundle:Plantilla:new.html.twig")
     */
    public function newAction()
    {
        $entity = new Plantilla();
        $form   = $this->createForm(new PlantillaType(), $entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }


    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ProyectoMainBundle:Plantilla')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Plantilla entity.');
        }
        
        $deleteForm = $this->createDeleteForm($id);
        $usuario=$this->getUser();
        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        );
    }

    
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ProyectoMainBundle:Plantilla')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Plantilla entity.');
        }

        return $this->redirect($this->generateUrl('plantilla_modificar', array('id' => $id)));
    }

    /**
     * Edits an existing Plantilla entity.
     *
     * @Route("/{id}", name="plantilla_update")
     * @Method("PUT")
     * @Template("ProyectoMainBundle:Plantilla:edit.html.twig")
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ProyectoMainBundle:Plantilla')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Plantilla entity.');
        }

        $editForm = $this->createForm(new PlantillaType(), $entity);
        $editForm->bind($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();
            
        }

        return $this->redirect($this->generateUrl('plantilla_modificar', array('id' => $id)));
    }

    /**
     * Deletes a Plantilla entity.
     *
     * @Route("/{id}", name="plantilla_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('ProyectoMainBundle:Plantilla')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Plantilla entity.');
            }

            $em->remove($entity);
            $em->flush();
        }
        $current_page=$this->getRequest()->request->get('current_page');
        return $this->redirect($this->generateUrl('plantilla_listar',array('current_page' => $current_page )));
    }

    /**
     * Creates a form to delete a Plantilla entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
    
     /**
     * Muestra el formulario para cargar la plantilla.
     *
     * @Route("/load", name="plantilla_load")
     * @Method("GET")
     * @Template("ProyectoMainBundle:Plantilla:load.html.twig")
     */
    public function loadAction()
    {
        $em = $this->getDoctrine()->getManager();

        $plantillas = $em->getRepository('ProyectoMainBundle:Plantilla')->findAll();
       
        return array(
            'plantillas' => $plantillas,
            
        );
    }
    
    
   
}
